#encoding:utf8
import requests
import re


# 由于直接用re库findall函数直接匹配，所以直接跳过网页解析，故不用BeautifulSoup库
def getH(url):  # 爬取淘宝网页功能模块
    try:  # 由于淘宝的防爬虫，所以将request对象中相应替换为以下headers,params
        headers = {
            'authority': 'www.taobao.com',
            'cache-control': 'max-age=0',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'referer': 'https://s.taobao.com/search?initiative_id=tbindexz_20170306&ie=utf8&spm=a21bo.21814703.201856-taobao-item.2&sourceId=tb.index&search_type=item&ssid=s5-e&commend=all&imgfile=&q=%E4%B9%A6%E5%8C%85&suggest=history_1&_input_charset=utf-8&wq=shu&suggest_query=shu&source=suggest',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cookie': 'cna=ykA8F8wwIhQCAXjnD2rV02VA; thw=cn; miid=139266461975127532; tg=0; tracknick=tb974603328; enc=9rD7NaiAOUBdZvdWQJKIu%2FGUAgOAk65Nba4MYm69MZXo7Ray9ygdQKPd2XuvRQVGqR7GH0gTbvJ7m8%2FsZtqtPw%3D%3D; hng=CN%7Czh-CN%7CCNY%7C156; xlly_s=1; t=122c996eaa771df2b9c5212871285f7f; lgc=tb974603328; mt=ci=16_1; sgcookie=E100jPR1J1pPkevdV2ZFyz%2FwUfWmW459FG7Szveibx30jPnr6QhkbdUtUo1RYJxZsUoZTFEjH8DwOvGNeU%2FeZsoRBA%3D%3D; uc3=id2=UNXzhB78YNdetg%3D%3D&nk2=F5RMHl6xd4zYCPg%3D&vt3=F8dCuwlBgA1irZ%2FnGlc%3D&lg2=WqG3DMC9VAQiUQ%3D%3D; uc4=nk4=0%40FY4HWyt7MS8T1L4kAACP7268%2F0kspA%3D%3D&id4=0%40UgJ2Y4N%2FZKxvkyJHZOxSebXBePGq; _cc_=UIHiLt3xSw%3D%3D; _m_h5_tk=37da3632b58750c3203d7d8e4cf1695d_1620038583219; _m_h5_tk_enc=d2e8836b07c7990128a398c9946d84d5; cookie2=1aa0712425dc9a6ee1b357eb62a1006b; _tb_token_=fee336363e833; _samesite_flag_=true; uc1=cookie14=Uoe2zXos9oCAhQ%3D%3D; isg=BFtbaBbCv23kbP0jbwSMGjbr_7_FMG8yu7vjuE2YK9pxLHsO1QLTgqauxoyiC8cq; tfstk=cbVPB3OR-_CzDWcCBbGeP4vIsRURwphn6nuIE-PzBybbd4f0ZH3XhxMqCaTSE; l=eBadA9elQG1Z3WlzBOfanurza77OSLAYYuPzaNbMiOCP_21B5_aRW61nuTY6C3GVh6mBR3S3Hn4XBeYBqQAonxvtIosM_Ckmn',
            'if-none-match': 'W/"1ad3f-vhpNj3xbY9jPgO6OC6hl7aXIaHE"',
        }
        params = (
            ('spm', 'a230r.1.1581860521.1.644e68b8B8D5oo'),
        )
        r = requests.get(url, headers=headers, params=params)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return ""


def parseH(ils, html):  # 将爬取内容转化为相应的数据结构的功能模块
    try:
        p = re.findall(r'"view_price":"[\d.]*"', html)  # re库的r.text表示方式
        t = re.findall(r'"raw_title":".*?"', html)
        for i in range(len(p)):
            price = eval(p[i].split(':')[1])
            title = eval(t[i].split(':')[1])
            ils.append([price, title])
    except:
        print("")


def printH(ils):
    temp = "{0:^6}\t{1:^8}\t{2:{3}^16}"  # 模板
    print(temp.format("序号", "价格", "商品名称", chr(12288)))
    count = 0
    for l in ils:
        count = count + 1
        print(temp.format(count, l[0], l[1], chr(12288)))


def main():
    sth = input("请输入要查找的商品名称：")
    pages = int(input("请输入要爬取的页面数："))  # 不建议爬取页面过多，以免造成页面崩溃
    aurl = 'https://s.taobao.com/search?q=' + sth
    inlist = []

    for i in range(pages):  # 对爬取每一页遍历，然后对每一页进行单一处理
        try:
            url = aurl + '&s=' + str(44 * i)  # 以爬取的每一页的URL进行访问爬取
            html = getH(url)
            parseH(inlist, html)
        except:
            continue
    printH(inlist)


main()
